﻿Imports System.ServiceModel.DomainServices.Client
Imports LullabyFullDeCaixa.Web

Partial Public Class DetallGestioHostals
    Inherits ChildWindow

#Region "Crear i Destruir"
    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub Child_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Closed
        Application.Current.RootVisual.SetValue(Control.IsEnabledProperty, True)
    End Sub

    Private Sub DetallGestioHostals_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        If RiaContext.Current.Authentication.User.Identity.IsAuthenticated = True Then
            Me.Activity.IsActive = True

            AddHandler Contexte.HostalsCarregats, AddressOf HostalsCarregats
            Contexte.LoadHostals()
        End If
    End Sub
#End Region

#Region "Guardar i Cancelar"
    Private Sub OKButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        EliminarObjectErrors()

        If LullabyFullDeCaixa.Contexte.DomainContext.EntityContainer.HasChanges Then
            If ValidarDades() Then
                LullabyFullDeCaixa.Contexte.DomainContext.SubmitChanges(AddressOf DomainContext_SubmittedChanges, Nothing)
                Activity.IsActive = True
            End If
        Else
            Me.DialogResult = True
        End If
    End Sub

    Private Function ValidarDades() As Boolean
        Dim Resultat As Boolean = True

        Return Resultat
    End Function

    Private Sub DomainContext_SubmittedChanges(ByVal s As SubmitOperation)
        Activity.IsActive = False

        If s.HasError Then
            If s.Error.Message.Contains("REFERENCE") Then
                Dim ValidationItem As New ValidationSummaryItem()
                ValidationItem.MessageHeader = StringLibrary.ImpossibleEliminar
                ValidationItem.Message = StringLibrary.DatosRelacionadosHostal
                ValidationItem.ItemType = ValidationSummaryItemType.ObjectError
                Validacio.Errors.Add(ValidationItem)

                s.MarkErrorAsHandled()
            Else
                App.ReportError(StringLibrary.ErrorGuardantDades)
            End If
        Else
            Me.DialogResult = True
        End If
    End Sub

    Private Sub CancelButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        If LullabyFullDeCaixa.Contexte.DomainContext.EntityContainer.HasChanges Then
            LullabyFullDeCaixa.Contexte.DomainContext.RejectChanges()
        End If

        Me.DialogResult = False
    End Sub

    Public Sub EliminarObjectErrors()
        Dim i As Long

        i = 0
        While i < Validacio.Errors.Count
            If Validacio.Errors(i).ItemType = ValidationSummaryItemType.ObjectError Then
                Validacio.Errors.RemoveAt(i)
            Else
                i = i + 1
            End If
        End While
    End Sub
#End Region

#Region "Carregar Dades"
    Private _HostalsCarregats As Boolean = False

    Public Sub HostalsCarregats(ByVal Resultat As Boolean, ByVal Missatge As String)
        RemoveHandler Contexte.HostalsCarregats, AddressOf HostalsCarregats
        Me.Activity.IsActive = False

        listHostals.ItemsSource = LullabyFullDeCaixa.Contexte.DomainContext.Hostals
    End Sub
#End Region

#Region "Boto Eliminar i afegir"

    Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim Hostal As Hostal = DirectCast(listHostals.SelectedItem, Hostal)

        LullabyFullDeCaixa.Contexte.DomainContext.Hostals.Remove(Hostal)

        'Sempre som administradors
        listHostals.ItemsSource = LullabyFullDeCaixa.Contexte.DomainContext.Hostals
    End Sub


    Private Sub btnAfegir_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim nouHostal As New LullabyFullDeCaixa.Web.Hostal() With {.Nom = "Default Hostal"}
        Contexte.DomainContext.Hostals.Add(nouHostal)

        'Sempre som administradors
        listHostals.ItemsSource = LullabyFullDeCaixa.Contexte.DomainContext.Hostals

        listHostals.SelectedItem = nouHostal

        txtNom.Focus()
        txtNom.SelectAll()
    End Sub

#End Region

    Private Sub listHostals_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
        If e.AddedItems.Count = 0 Then
            Me.gridDetall.Visibility = Windows.Visibility.Collapsed
            Me.btnEliminar.IsEnabled = False
        Else
            Me.gridDetall.Visibility = Windows.Visibility.Visible
            Me.btnEliminar.IsEnabled = True
        End If
    End Sub

    Private Sub Scroll_SizeChanged(ByVal sender As System.Object, ByVal e As System.Windows.SizeChangedEventArgs)
        Me.Components.MaxHeight = Math.Max(Me.Scroll.ViewportHeight, Me.Components.MinHeight)
        Me.Components.MaxWidth = Math.Max(Me.Scroll.ViewportWidth, Me.Components.MinWidth)
    End Sub

End Class

